package com.rain.cy.business.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.google.common.base.Preconditions;
import com.rain.cy.business.constant.DicCategoryConstant;
import com.rain.cy.business.model.dto.PatientFeedbackDTO;
import com.rain.cy.business.model.dto.UserProcessDataDTO;
import com.rain.cy.business.model.entity.UserEntity;
import com.rain.cy.business.model.entity.UserFeedbackEntity;
import com.rain.cy.business.model.entity.UserProcessDataEntity;
import com.rain.cy.business.model.entity.UserProcessStatusEntity;
import com.rain.cy.business.service.*;
import com.rain.cy.framework.entity.BaseController;
import com.rain.cy.framework.entity.BaseEntity;
import com.rain.cy.framework.result.Result;
import com.rain.cy.framework.result.ResultUtil;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author yu_chen
 * @since 2021/10/9 23:02
 */
@RestController
@RequestMapping("api/process")
public class ProcessController extends BaseController {

    @Autowired
    private UserProcessDataService userProcessDataService;

    @Autowired
    private UserFeedbackService userFeedbackService;

    @Autowired
    private ActionCountService actionCountService;

    @Autowired
    private UserProcessStatusService userProcessStatusService;

    @Autowired
    private UserService userService;

    @Autowired
    private DictionaryService dictionaryService;

    @GetMapping("user/query")
    public Result queryUserProcess(Integer userId) {
        Preconditions.checkArgument(userId != null, "用户ID不能为null");
        UserProcessStatusEntity processStatusEntity = userProcessStatusService.lambdaQuery().eq(UserProcessStatusEntity::getUserId, userId).last("limit 1").one();
        if (processStatusEntity != null) {
            return ResultUtil.success(userProcessStatusService.buildTree(processStatusEntity));
        }
        UserProcessStatusEntity userProcessStatusEntity = userProcessStatusService.getAndInitUserProcessStatus(userId);
        return ResultUtil.success(userProcessStatusService.buildTree(userProcessStatusEntity));
    }


    @PostMapping("/data/option")
    @ApiOperation(value = "保存或者更新接口")
    public Result saveProcessData(@RequestBody UserProcessDataDTO userProcessDataDTO) {
        Preconditions.checkArgument(userProcessDataDTO.getUserId() != null, "用户ID不能为null");
        Preconditions.checkArgument(StringUtils.isNotEmpty(userProcessDataDTO.getProcessId()), "processId不能为null");
        Preconditions.checkArgument(StringUtils.isNotEmpty(userProcessDataDTO.getProcessData()), "processData不能为null");
        UserProcessDataEntity userProcessDataEntity = BeanUtil.copyProperties(userProcessDataDTO, UserProcessDataEntity.class);
        return ResultUtil.success(userProcessDataService.saveOrUpdateHandler(userProcessDataEntity));
    }

    @GetMapping("/data/one")
    @ApiOperation(value = "查询单条记录")
    public Result queryOne(UserProcessDataDTO userProcessDataDTO) {
        Preconditions.checkArgument(userProcessDataDTO.getUserId() != null, "用户ID不能为null");
        Preconditions.checkArgument(userProcessDataDTO.getProcessId() != null, "processId不能为null");
        List<UserProcessDataDTO> userProcessDataList = userProcessDataService.queryUserProcessData(userProcessDataDTO.getUserId(), userProcessDataDTO.getProcessId());
        if (CollectionUtil.isNotEmpty(userProcessDataList)) {
            return ResultUtil.success(userProcessDataList.get(0));
        }
        return ResultUtil.success();
    }

    @GetMapping("/data/list")
    @ApiOperation(value = "查询多条记录")
    public Result queryList(UserProcessDataDTO userProcessDataDTO) {
        Preconditions.checkArgument(userProcessDataDTO.getUserId() != null, "用户ID不能为null");
        Preconditions.checkArgument(userProcessDataDTO.getProcessId() != null, "processId不能为null");
        List<UserProcessDataDTO> userProcessDataList = userProcessDataService.queryUserProcessData(userProcessDataDTO.getUserId(), userProcessDataDTO.getProcessId(), true);
        return ResultUtil.success(userProcessDataList);
    }

    @PostMapping("/patient/feedback")
    @ApiOperation(value = "患者反馈数据")
    public Result saveFeedback(@RequestBody PatientFeedbackDTO patientFeedbackDTO) {
        Integer currentUserId = getCurrentUserId();
        UserEntity userEntity = userService.getById(currentUserId);
        UserFeedbackEntity userFeedbackEntity = BeanUtil.copyProperties(patientFeedbackDTO, UserFeedbackEntity.class);
        userFeedbackEntity.setUserId(currentUserId);
        userFeedbackEntity.setUserName(userEntity.getName());
        userFeedbackEntity.setCheckType(userEntity.getCheckType());
        userFeedbackEntity.setDoctorId(userEntity.getDoctorId());
        boolean save = userFeedbackService.save(userFeedbackEntity);
        if (save) {
            actionCountService.plusSendFeedbackNum(currentUserId);
        }
        return ResultUtil.success();
    }


    @GetMapping("/patient/feedback/query")
    @ApiOperation(value = "患者反馈数据")
    public Result queryFeedback(Integer userId, String processId) {
        UserFeedbackEntity feedbackEntity = userFeedbackService.lambdaQuery()
                .eq(UserFeedbackEntity::getUserId, userId)
                .eq(UserFeedbackEntity::getProcessId, processId)
                .orderByDesc(BaseEntity::getCreateTime)
                .last("limit 1").one();
        if (feedbackEntity != null) {
            String dictionaryValueByType = dictionaryService.getDictionaryValueByType(DicCategoryConstant.USER_FEEDBACK_TAG, feedbackEntity.getTag());
            feedbackEntity.setTagValue(dictionaryValueByType);
        }
        return ResultUtil.success(feedbackEntity);
    }
}
